package com.example.mymvp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.mymvp.Presenter.MyPresenterIml;
import com.example.mymvp.View.MyView;

public class MainActivity extends AppCompatActivity implements MyView, View.OnClickListener {


    private Button login;
    private Button ckear;
    private EditText name;
    private EditText pas;
    private MyPresenterIml myPresenterIml;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.setTitle("登录");
        myPresenterIml = new MyPresenterIml(this);
        initView();
    }

    private void initView() {

        name = findViewById(R.id.et_name);
        pas = findViewById(R.id.et_psw);
        login = findViewById(R.id.btn_login);
        ckear = findViewById(R.id.btn_ckear);

        login.setOnClickListener(this);
        ckear.setOnClickListener(this);
    }


    @Override
    public void onClearText() {
        name.setText("");
        pas.setText("");
    }

    @Override
    public void onLoginReuslt(Boolean res, int code) {
        switch (code) {
            case 0:
                Toast.makeText(this, "请输入账号，密码", Toast.LENGTH_SHORT).show();
                break;
            case 1:
                Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
                onClearText();
                break;
            case -3:
                Toast.makeText(this, "用户名密码都错了，请仔细填写", Toast.LENGTH_SHORT).show();
                onClearText();
                break;
            case -1:
                Toast.makeText(this, "用户名错误", Toast.LENGTH_SHORT).show();
                name.setText("");
                break;
            case -2:
                Toast.makeText(this, "密码错误", Toast.LENGTH_SHORT).show();
                pas.setText("");
                break;

        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:
                String iName = name.getText().toString();
                String iPas = pas.getText().toString();
                myPresenterIml.doLongin(iName, iPas);
                break;
            case R.id.btn_ckear:
                myPresenterIml.clear();
                break;
        }
    }
}
